<?php 

namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Model\Album;
use Album\Form\AlbumForm;

class AlbumController extends AbstractActionController
{
    protected $albumTable;
	public function indexAction()
	{
	    $mathches  =   $this->getEvent()->getRouteMatch();	   
	    $page     =   $mathches->getParam('id',1);	   
	    $iteratorAdapter   =   new \Zend\Paginator\Adapter\Iterator($this->getAlbum()->fetchAll());
	    $paginator =   new \Zend\Paginator\Paginator($iteratorAdapter);
	    $paginator->setCurrentPageNumber($page);
	    $paginator->setItemCountPerPage(2);
	    
	    return new ViewModel(array(
	    	'albums'=>$paginator
	    ));
	}

	public function addAction()
	{
	    $form = new AlbumForm();
	    $form->get('submit')->setValue('Add');
	    
	    $request = $this->getRequest();
	    if ($request->isPost()) {
	    	$album = new Album();
	    	$form->setInputFilter($album->getInputFilter());
	    	$form->setData($request->getPost());
	    
	    	if ($form->isValid()) {
	    		$album->exchangeArray($form->getData());
	    		$this->getAlbum()->saveAlbum($album);
	    
	    		// Redirect to list of albums
	    		return $this->redirect()->toRoute('album');
	    	}
	    }
	    return array('form' => $form);
	     
	}

    public function editAction()
     {
         $id = (int) $this->params()->fromRoute('id', 0);
         if (!$id) {
             return $this->redirect()->toRoute('album', array(
                 'action' => 'add'
             ));
         }

         // Get the Album with the specified id.  An exception is thrown
         // if it cannot be found, in which case go to the index page.
         try {
             $album = $this->getAlbum()->getAlbum($id);
         }
         catch (\Exception $ex) {
             return $this->redirect()->toRoute('album', array(
                 'action' => 'index'
             ));
         }

         $form  = new AlbumForm();
         $form->bind($album);
         $form->get('submit')->setAttribute('value', 'Edit');

         $request = $this->getRequest();
         if ($request->isPost()) {
             $form->setInputFilter($album->getInputFilter());
             $form->setData($request->getPost());

             if ($form->isValid()) {
                 $this->getAlbum()->saveAlbum($album);

                 // Redirect to list of albums
                 return $this->redirect()->toRoute('album');
             }
         }

         return array(
             'id' => $id,
             'form' => $form,
         );
     }

	public function deleteAction()
	{

	    $id = (int) $this->params()->fromRoute('id', 0);
	    if (!$id) {
	    	return $this->redirect()->toRoute('album');
	    }
	    
	    $request = $this->getRequest();
	    if ($request->isPost()) {
	    	$del = $request->getPost('del', 'No');
	    
	    	if ($del == 'Yes') {
	    		$id = (int) $request->getPost('id');
	    		$this->getAlbum()->deleteAlbum($id);
	    	}
	    
	    	// Redirect to list of albums
	    	return $this->redirect()->toRoute('album');
	    }
	    
	    return array(
	    		'id'    => $id,
	    		'album' => $this->getAlbum()->getAlbum($id)
	    );
	}
	
	public function getAlbum(){
	    if(!$this->albumTable){
	        $sm    =   $this->getServiceLocator();
	        $this->albumTable  =   $sm->get('Album\Model\AlbumTable');
	    }
	    return $this->albumTable;
	}
}